const app = new Vue({
  el: '#app',
  data: {
    books: [
      {id: 1, name: '《算法导论》', date: '2006-9', price: 85.00, count: 1},
      {id: 2, name: '《UNIX编程艺术》', date: '2006-2', price: 59.00, count: 1},
      {id: 3, name: '《编程珠玑》', date: '2008-10', price: 39.00, count: 1},
      {id: 4, name: '《代码大全》', date: '2006-3', price: 128.00, count: 1},
    ],
  },
  computed: {
    totalPrice() {
      let result = 0;
      /*for(let book of this.books){
        result += book.count * book.price;
      }*/

      /*result = this.books.reduce(function (preValue, book) {
        return preValue + book.count * book.price
      },0)*/

      result = this.books.reduce((preValue, book) => preValue + book.count * book.price,0);

      return result;
    }
  },
  methods: {
    decrement(index) {
      // if (this.books[index].count > 1) {
      this.books[index].count--
      // }
    },
    increment(index) {
      this.books[index].count++
    },
    clear(index) {
      this.books.splice(index, 1)
    }
  },
  filters: { // vue过滤器的用法
    showPrice(price) {
      return '￥' + price.toFixed(2);
    }
  }
});
